import {Buffer} from "buffer";
import {Unsigned16} from "./Unsigned16";
import {OBI} from "./OBI";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Unsigned8} from "./Unsigned8";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";


export class OAD implements AxdrType {

    dataCode: Buffer | null = null;
    oi: Unsigned16 | null = null;
    obi: OBI | null = null;
    index: Unsigned16 | null = null;

    constructor() {
    }

    set_dataCode(dataCode: Buffer) {
        this.dataCode = dataCode;
    }

    set_oad(oi: Unsigned16, obi: OBI, index: Unsigned8) {
        this.oi = oi;
        this.obi = obi;
        this.index = index;
    }

    getOI(): Unsigned16 {
        if (this.oi != null) {
            return this.oi;
        } else {
            throw new Error("oi is null");
        }
    }

    getOBI(): OBI {
        if (this.obi != null) {
            return this.obi;
        } else {
            throw new Error("obi is null");
        }
    }

    getIndex(): Unsigned8 {
        if (this.index != null) {
            return this.index;
        } else {
            throw new Error("index is null");
        }
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess)
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        this.oi = new Unsigned16();
        this.oi.set_const();
        codeLength += this.oi.decode(input);

        this.obi = new OBI();
        this.obi.setNumBit(8);
        codeLength += this.obi.decode(input);

        this.index = new Unsigned8();
        this.index.set_const();
        codeLength += this.index.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.oi != null && this.obi != null && this.index != null) {
                codeLength = 0;
                codeLength += this.index.encode(output);
                codeLength += this.obi.encode(output);
                codeLength += this.oi.encode(output);
            } else {
                throw new Error("oi || obi || index is null");
            }
        }
        return codeLength;
    }

    toString(): string {
        if (this.oi != null && this.obi != null && this.index != null) {
            return "sequence: {" + "oi: " + this.oi + ", obi: " + this.obi + ", index: " + this.index + "}";
        } else {
            return "oi || obi || index is null";
        }
    }


}

//
// let oad = new OAD();
// oad.set_oad(new Unsigned16(), new OBI(), new Unsigned8());
// console.log(oad.getOBI());
